module executor.user.UserExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import std.array;
import std.conv : to;
import hunt.util.Serialize;
import service.OfflineService;
import service.SearchUserService;

/**
用户信息模块
**/

@TransportServer(LogicServerName)
class UserExecutor : AbstractExecutor!(UserExecutor){

  //设置用户离线接口
  @MessageId(MESSAGE.CMD_OFFLINE)
  void userOffline(TransportContext ctx, MessageBuffer buffer) {
    string userId = cast(string)buffer.data;
    OfflineService.userOffline(userId);
  }

  //生成用户二维码接口
  @MessageId(MESSAGE.CMD_MAKE_QRCODE_REQ)
  void makeQrcode(TransportContext ctx, MessageBuffer buffer) {
    MakeQrCodeREQ req = new MakeQrCodeREQ;
    buffer.data.fromProtobuf!MakeQrCodeREQ(req);

    MakeQrCodeRES res = new MakeQrCodeRES;
    string code = SearchUserService.makeQrcode(req.userId);
    res.userId = req.userId;
    res.code = code;
    res.tagId = req.tagId;

    ctx.session().send(new MessageBuffer(MESSAGE.CMD_MAKE_QRCODE_RES,res.toProtobuf.array));
  }

  //根据二维码获取用户信息接口
  @MessageId(MESSAGE.CMD_GET_UER_BY_QRCODE_REQ)
  void getUserByQrCode(TransportContext ctx, MessageBuffer buffer) {
    GetUserByQrCodeREQ req = new GetUserByQrCodeREQ;
    buffer.data.fromProtobuf!GetUserByQrCodeREQ(req);

    auto user = SearchUserService.selectUserByQrCode(req.code,req.userId);

    GetUserByQrCodeRES res = new GetUserByQrCodeRES;
    res.userId = req.userId;
    res.src = req.src;
    res.tagId = req.tagId;
    res.user = user;
    ctx.session().send(new MessageBuffer(MESSAGE.CMD_GET_UER_BY_QRCODE_RES,res.toProtobuf.array));
  }
}

